import { queryPostTemplate } from '@/services/api';
import { useQuery } from '@tanstack/react-query';
import type { IPostTemplate } from '@/interfaces';
import { useContext } from 'react';
import Alert from '@/app/[locale]/alert/alert';
import AlertLoad from '@/app/[locale]/alert/load';
import QueryPostTemplateInfo from '@/app/[locale]/admin/forum/postTemplate/detail/info';
import { PostTemplateAdminContext } from '@/contexts/admin/forum/postTemplate';
import QueryPostTemplateContent from '@/app/[locale]/admin/forum/postTemplate/detail/content';

export default function DetailsPostTemplateAdminPage() {
  const context = useContext(PostTemplateAdminContext);

  const queryPostTemplateQuery = useQuery(
    ['/forum', '/templates', context.detailItem?.id],
    async (context) => {
      return (await queryPostTemplate({
        id: context.queryKey[2] + '',
      })) as IPostTemplate;
    },
  );

  if (queryPostTemplateQuery.data) {
    const data = queryPostTemplateQuery.data;

    return (
      <div className="row">
        <div className="col">
          <div className="vstack gap-4">
            <QueryPostTemplateInfo details={data} />

            <QueryPostTemplateContent details={data} />
          </div>
        </div>
      </div>
    );
  }

  if (queryPostTemplateQuery.error) {
    return <Alert type="error" message={queryPostTemplateQuery.error} />;
  }

  return <AlertLoad />;
}
